package mt.dummy;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.rmi.server.ServerCloneException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import mt.Order;
import mt.comm.ClientComm;
import mt.comm.ClientSideMessage;
import mt.comm.ServerSideMessage;
import mt.comm.ServerSideMessage.Type;

public class ClientCommDummy implements ClientComm {

	private BlockingQueue<ServerSideMessage> sendMessageList;
	private BlockingQueue<ClientSideMessage> receiveMessageList;
	private String nickname;
	private Socket socket;
	
	@Override
	public void connect(String serverAddress, String nickname) {
		this.nickname = nickname;
		sendMessageList = new LinkedBlockingQueue<ServerSideMessage>();
		receiveMessageList = new LinkedBlockingQueue<ClientSideMessage>();

		InetAddress address;
		try {
			address = InetAddress.getByName(serverAddress);
			//address = InetAddress.getByName("127.0.0.1");
			socket = new Socket(address, ServerCommDummy.PORT);


		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void disconnect() {
		
		try {
			socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public ClientSideMessage getNextMessage() {
		ClientSideMessage cm = null;
		try {
			cm = receiveMessageList.take();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cm;
	}

	@Override
	public boolean hasNextMessage() {
		return !receiveMessageList.isEmpty();
	}

	@Override
	public boolean isConnected() {
		return !socket.isClosed();
	}

	@Override
	public void sendOrder(Order order) {
		//getSendMessageList().add(new ServerSideMessage(Type.NEW_ORDER,nickname, this.nickname );


	}

	public BlockingQueue<ServerSideMessage> getSendMessageList() {
		return sendMessageList;
	}

	public BlockingQueue<ClientSideMessage> getReceiveMessageList() {
		return receiveMessageList;
	}
	

}
